#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <sys/time.h>

#include "bpt.h"

#define NUMBER_OPS 10000000

#define VERBOSE_TEST false

int main (int argc, char const *argv[])
{
	if(argc != 1) {
		printf("Unknown argument: %s\n", argv[1]);
		exit(1);
	}
	
	node * root = NULL;
	record * r = NULL;
	
	print_stats();
	
	
	printf("Performing %d insert, %d delete, %d find\n", 
		NUMBER_OPS, NUMBER_OPS/2, NUMBER_OPS);
	
	struct timeval start_time;
	struct timeval end_time;
	
	gettimeofday(&start_time, NULL);
	
	unsigned i;
	for(i = 1; i < NUMBER_OPS+1; i++)	{
		if(VERBOSE_TEST) printf("Insert %d\n", i);
		root = insert(root, i, i);		
	}

	for(i = 1; i < NUMBER_OPS; i += 2)	{
		if(VERBOSE_TEST) printf("Delete %d\n", i);
		root = delete(root, i);

		if(VERBOSE_TEST) printf("Find %d\n", i);
		r = find(root, i);
		assert(r == NULL);

		if(VERBOSE_TEST) printf("Find %d\n", i+1);
		r = find(root, i+1);
		assert(r != NULL);
	}


	gettimeofday(&end_time, NULL);
	
	unsigned tot_ops = (NUMBER_OPS + (NUMBER_OPS/2) + NUMBER_OPS);
	unsigned duration_msec = 0;
	duration_msec += (end_time.tv_sec - start_time.tv_sec) * 1000;
	duration_msec += (end_time.tv_usec - start_time.tv_usec) / 1000;
	
	printf("Duration %u milliseconds\n", duration_msec);
	
	printf("%.2f operations/s\n", ((float)tot_ops*1000)/duration_msec);
	
	return 0;
}
